import { Refund } from 'refund-model.js';
var refund = new Refund();
Page({
  data: {
    company: ["顺丰", "申通", "中通"],
    index : '',
    info:{
      created_at: 12904262
    }
  },
  onLoad: function (options) {
    this.data.id = options.id;
    if (!this.data.id){
      return false;
    }
    this.getCompany();
  },
  
  //获取快递公司列表
  getCompany: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var args = {
      id: this.data.id
    }
    refund.expressCompany(args, (res) => {
      that._loadData();
      var data = res.items;
      that.setData({
        company: data,
      })
    });
  },
  //获取详情
  _loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var args = {
      id: this.data.id
    }
    refund.info(args, (res) => {
      wx.hideLoading()
      var data = res.item;
      that.setData({
        info: data,
      })
    });
  },
  //提交发货单
  sub: function () {
    var that = this;
    var index = this.data.index;
    if (!this.data.code || !index){
      wx.showToast({
        title: '请选择快递公司，并填写快递单号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var args = {
      id: this.data.id,
      delivery_no: this.data.code,
      company: this.data.company[index].code,
    }
    refund.fahuo(args, (res) => {
      if(res.status == 1){
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success:function(){
            setTimeout(function () {
              wx.redirectTo({
                url: 'refund?index=1',
              })
            }, 2000)
          }
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

  //获取快递单号输入内容
  code: function (e) {
    var code = e.detail.value;
    this.data.code = code
  },
  //快递公司
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
})